package com.fjzx.androidframe.global.util;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;
import android.text.format.Formatter;

/**
 * 手机硬件信息工具类
 *
 * @author xiongyongcong
 */
public class XDeviceUtil {
    /**
     * 获取App版本
     *
     * @param context
     * @return
     */
    public static int getAppVersion(Context context) {
        try {
            int versionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
            return versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取Android版本
     * @return
     */
    public static int getAndroidVersion() {
        return Build.VERSION.SDK_INT;
    }

    /**
     * 获取手机IMEI号
     * @param context
     * @return
     */
    @SuppressLint("MissingPermission")
    public static String getImei(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            return telephonyManager.getImei();
        } else {
            return telephonyManager.getDeviceId();
        }
    }

    /**
     * 获取设备厂商
     * @return
     */
    public static String getBrand() {
        return Build.BRAND;
    }

    /**
     * 获取设备类型
     * @return
     */
    public static String getModel() {
        return Build.MODEL;
    }

    /**
     * 获取Cpu架构
     * @return
     */
    public static String getCpuInfo() {
        String[] cpuInfo;
        if (Build.VERSION.SDK_INT > 21) {
            cpuInfo = Build.SUPPORTED_ABIS;
        } else {
            cpuInfo = new String[]{Build.CPU_ABI, Build.CPU_ABI2};
        }

        StringBuilder sb = new StringBuilder();
        for (String info : cpuInfo) {
            sb.append(info);
        }
        return sb.toString();
    }

    /**
     * 获取可用内存大小
     * @param context
     * @return
     */
    public static long getAvailMemorySize(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(memInfo);
        return memInfo.availMem;
    }

    /**
     * 获取全部内存大小
     * @param context
     * @return
     */
    public static long getTotalMemorySize(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(memInfo);
        return memInfo.totalMem;
    }

    public static String getMemoryInfo(Context context) {
        long totalMemory = getTotalMemorySize(context);
        long availMemory = getAvailMemorySize(context);
        String total = Formatter.formatFileSize(context, totalMemory);
        String avail = Formatter.formatFileSize(context, availMemory);
        return avail + "(可用)/" + total + "(总)";
    }

    /**
     * 获取运营商
     * @param context
     * @return
     */
    public static String getOperatorType(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String operator = tm.getNetworkOperator();
        String opeType = "未知";
        if ("46001".equals(operator) || "46006".equals(operator) || "46009".equals(operator)) {
            opeType = "中国联通";
        } else if ("46000".equals(operator) || "46002".equals(operator) || "46004".equals(operator) || "46007".equals(operator)) {
            opeType = "中国移动";
        } else if ("46003".equals(operator) || "46005".equals(operator) || "46011".equals(operator)) {
            opeType = "中国电信";
        }
        return opeType;
    }

    /**
     * 获取网络类型
     * @param context
     * @return
     */
    @SuppressLint("MissingPermission")
    public static String getNetworkType(Context context){
        String networkType = "";
        ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (context == null || manager == null){
            networkType = "null";
        }
        NetworkInfo info = manager.getActiveNetworkInfo();
        if (info != null && info.isAvailable()){
            if (manager != null){
                int subType = info.getSubtype();
                if(info.getType() ==  ConnectivityManager.TYPE_MOBILE){
                    if (subType == TelephonyManager.NETWORK_TYPE_CDMA || subType == TelephonyManager.NETWORK_TYPE_EDGE || subType == TelephonyManager.NETWORK_TYPE_GPRS){
                        networkType = "2G";
                    }else if (subType == TelephonyManager.NETWORK_TYPE_UMTS || subType == TelephonyManager.NETWORK_TYPE_EVDO_0 || subType == TelephonyManager.NETWORK_TYPE_EVDO_A || subType == TelephonyManager.NETWORK_TYPE_EVDO_B || subType == TelephonyManager.NETWORK_TYPE_HSDPA){
                        networkType = "3G";
                    }else if (subType == TelephonyManager.NETWORK_TYPE_LTE){
                        networkType = "4G";
                    }
                } else if (info.getType() == ConnectivityManager.TYPE_WIFI){
                    networkType = "wifi";
                }
            }
        }
        return networkType;
    }

    @SuppressLint("MissingPermission")
    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    @SuppressLint("MissingPermission")
    public static String getMacAddr(Context context) {
        WifiManager wm = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        String mac = wm.getConnectionInfo().getMacAddress();
        return mac == null ? "" : mac;
    }

    /**
     * SD卡是否可用
     * @return
     */
    public static boolean isSDCardUsable() {
        boolean SDCardMounted = false;
        String sDStateString = Environment.getExternalStorageState();
        if (sDStateString.equals(Environment.MEDIA_MOUNTED)) {
            SDCardMounted = true;
        }

        // 是否正在检测SD卡
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_CHECKING)
                || Environment.getExternalStorageState().equals(Environment.MEDIA_NOFS)) {
            SDCardMounted = false;
        }

        // 检测是否插有SD卡
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)
                || Environment.getExternalStorageState().equals(Environment.MEDIA_UNMOUNTED)) {
            SDCardMounted = false;
        }

        // 检测SD卡是否连接电脑共享
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_SHARED)) {
            SDCardMounted = false;
        }
        return SDCardMounted;
    }

    /**
     * 获取SDCard剩下的大小
     *
     * @return SDCard剩下的大小
     */
    public static long getSDCardRemainSize() {
        StatFs statfs = new StatFs(Environment.getExternalStorageDirectory().getPath());
        long blockSize = statfs.getBlockSize();
        long availableBlocks = statfs.getAvailableBlocks();
        return availableBlocks * blockSize;
    }
}
